package com.xingkeduo.business.order.controller;

import com.alibaba.fastjson.JSON;
import com.xingkeduo.business.order.domain.OrderDomainService;
import com.xingkeduo.business.order.domain.OrderQueryService;
import com.xingkeduo.dto.param.PaymentParam;
import com.xingkeduo.dto.response.OrderAdminDetailResponseDto;
import com.xingkeduo.dto.response.OrderPayStatusDto;
import com.xingkeduo.utils.ResponseUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.xingkeduo.utils.Constant.UnCheck_URL_PREFIX;

/**
 * @author chi  2019-08-07 17:53
 **/
@RestController
@RequestMapping(UnCheck_URL_PREFIX + "/order")
@Slf4j
public class UncheckOrderController {


    @Resource
    private OrderDomainService orderDomainService;

    @Resource
    private OrderQueryService orderQueryService;


    @GetMapping("/paid/status")
    public ResponseEntity paidStatus(String id) {
        log.info("paid order:{}", JSON.toJSONString(id));
        OrderPayStatusDto orderPayStatusDto = orderDomainService.paidStatus(id);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderPayStatusDto), HttpStatus.OK);
    }


    /**
     * 发型师端 订单详情
     */
    @GetMapping("/admin/query/detail")
    public ResponseEntity detailQuery(String id,String orderNo) {
        log.info("admin query detail:{},{}", id, orderNo);
        OrderAdminDetailResponseDto orderAdminResponseDto = orderQueryService.adminDetail(id, orderNo);
        return new ResponseEntity<>(ResponseUtils.getSuccess(orderAdminResponseDto), HttpStatus.OK);
    }

    /**
     * 调用支付
     * @param paymentParam
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/to_pay")
    public ResponseEntity toPay(@RequestBody PaymentParam paymentParam, HttpServletRequest httpServletRequest){
        ResponseUtils responseUtils = orderDomainService.toPay(paymentParam, getCustomerIp(httpServletRequest));
        return new ResponseEntity<>(responseUtils, HttpStatus.OK);
    }


    /**
     * 获取签署者IP
     *
     * @param request 请求地址
     * @return
     */
    private String getCustomerIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Real-IP");
        if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
            log.info("X-Real-IP" + " " + ip);
            return ip;
        }
        ip = request.getHeader("X-Forwarded-For");
        if (ip != null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个IP值，第一个为真实IP。
            int index = ip.indexOf(',');
            if (index != -1) {
                return ip.substring(0, index);
            } else {
                log.info("X-Forwarded-For" + " " + ip);
                return ip;
            }
        } else {
            log.info("getRemoteAddr" + " " + request.getRemoteAddr());
            return request.getRemoteAddr();
        }
    }

}
